//

// (  )
//

package com.tools.acceleratecore.xlib.xlib.impl;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import com.tools.acceleratecore.xlib.xlib.intf.IXThreadTaskQueue;
import com.tools.acceleratecore.xlib.xlib.intf.IXThreadTaskQueueListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class XThreadTaskQueue extends XObserver<IXThreadTaskQueueListener> implements IXThreadTaskQueue {
    private Thread mThread = null;
    private Runnable mRunnable = null;
    private Handler mHandler = null;
    private boolean mWork = false;
    private List<Object> mListObjectTag = null;
    private Object mObjLock = null;
    private static final int VALUE_INT_MESSAGEID_THREAD_TASK_QUEUE = 4096;

    public XThreadTaskQueue() {
        this._init();
    }

    private void _init() {
        this.mObjLock = new Object();
        this.mListObjectTag = new ArrayList();
        this.mHandler = new Handler(Looper.getMainLooper()) {
            public void handleMessage(Message msg) {
                if (4096 == msg.what) {
                    List var2 = XThreadTaskQueue.this.mListListener;
                    synchronized(XThreadTaskQueue.this.mListListener) {
                        Iterator var4 = XThreadTaskQueue.this.getListListener().iterator();

                        while(var4.hasNext()) {
                            IXThreadTaskQueueListener listener = (IXThreadTaskQueueListener)var4.next();
                            listener.onThreadComplete(msg.obj);
                        }
                    }
                }

            }
        };
        this.mRunnable = new Runnable() {
            public void run() {
                while(true) {
                    Object objectTag = null;
                    synchronized(XThreadTaskQueue.this.mListObjectTag) {
                        if (XThreadTaskQueue.this.mListObjectTag.size() == 0) {
                            try {
                                XThreadTaskQueue.this.mListObjectTag.wait();
                            } catch (Exception var5) {
                                var5.printStackTrace();
                            }
                        }

                        objectTag = XThreadTaskQueue.this.mListObjectTag.get(0);
                        XThreadTaskQueue.this.mListObjectTag.remove(0);
                    }

                    List var2 = XThreadTaskQueue.this.mListListener;
                    synchronized(XThreadTaskQueue.this.mListListener) {
                        Iterator var4 = XThreadTaskQueue.this.getListListener().iterator();

                        while(true) {
                            if (!var4.hasNext()) {
                                break;
                            }

                            IXThreadTaskQueueListener listener = (IXThreadTaskQueueListener)var4.next();
                            listener.onThreadWork(objectTag);
                        }
                    }

                    Message msg = new Message();
                    msg.what = 4096;
                    msg.obj = objectTag;
                    XThreadTaskQueue.this.mHandler.sendMessage(msg);
                }
            }
        };
    }

    public boolean start() {
        Object var1 = this.mObjLock;
        synchronized(this.mObjLock) {
            if (this.mWork) {
                return false;
            } else {
                this.mWork = true;
                this.mThread = new Thread(this.mRunnable);
                this.mThread.start();
                return true;
            }
        }
    }

    public void addTask(Object objectTag, boolean bFirst) {
        List var3 = this.mListObjectTag;
        synchronized(this.mListObjectTag) {
            if (bFirst) {
                this.mListObjectTag.add(0, objectTag);
            } else {
                this.mListObjectTag.add(objectTag);
            }

            this.mListObjectTag.notify();
        }
    }

    public void stop() {
        Object var1 = this.mObjLock;
        synchronized(this.mObjLock) {
            if (this.mWork && this.mThread != null) {
                this.mThread.interrupt();
            }
        }

        this.clear();
    }

    public boolean isWork() {
        Object var1 = this.mObjLock;
        synchronized(this.mObjLock) {
            return this.mWork;
        }
    }

    private void clear() {
        Object var1 = this.mObjLock;
        synchronized(this.mObjLock) {
            this.mWork = false;
            this.mThread = null;
        }
    }
}
